#include "../TimeManager/timethread.h"
extern int d_system_type;
timethread::timethread(QObject *parent) : QObject(parent)
{

        switch(d_system_type)
        {
         case WINDOWS:
         {
          m_operationworker = new Operation_Windows();
         }
           break;
         case Linux:
          m_operationworker = new Operation_Linux();
            break;
         default:break;
        }

}
void timethread::ct_time()
{

    nowtime = QDateTime::currentDateTime();
    atime.systime=nowtime;



    if(myshut.type!=0)
    {
        nowtime_t = nowtime.toTime_t();
//        qDebug()<<"nowtime_t:"<<nowtime_t<<"sreentime_t:"<<sreentime_t<<endl;
        if(qAbs(sreentime_t-nowtime_t)<2)
            MyScreenShutDown(true);

         if(myshut.type==1)
         {
              nowtime_t = nowtime.toTime_t();
              QDateTime endtime;
              endtime.setTime(myshut.endtime);
             if(QTime::currentTime().secsTo(myshut.endtime)<0)
                 endtime.setDate(QDate::currentDate().addDays(1));
             else
                 endtime.setDate(QDate::currentDate());

            endtime_t= endtime.toTime_t();
         }
         else if(myshut.type==2)
         {
             if(endtime_t==0)
            {
              endtime_t =nowtime.toTime_t()+myshut.endtime.minute()*60+myshut.endtime.hour()*3600;
//              qDebug()<<"end time:"<<QDateTime::fromTime_t(endtime_t).toString("MM/dd hh:mm:ss")<<endl;
            }
         }
        int hour=(endtime_t-nowtime_t)/3600;
        int Minute= (endtime_t-nowtime_t)%3600/60;
        int sencond=(endtime_t-nowtime_t)%3600%60;

        QTime Countdowntime(hour,Minute,sencond,0);

  //       qDebug()<<Countdowntime<<endl;
         if(hour==0&&Minute==0&&sencond==0)
         {
             MySystemShutDown();
         }

        atime.Countdowntime=Countdowntime;
    }
    emit time_Signal(atime);

}
void timethread::shotdown(c_type killtime)
{
  if(killtime.type==3)
  {
      qDebug()<<killtime.endtime<<endl;
      sreentime_t =QDateTime::currentDateTime().toTime_t()+killtime.endtime.minute()*60+killtime.endtime.hour()*3600;
      qDebug()<<"屏幕将在:"<<QDateTime::fromTime_t(sreentime_t).toString("MM/dd hh:mm:ss")<<"关闭"<<endl;

      if(myshut.type==0)
          myshut.type=3;
  }
  else if(killtime.type==0)
  {
   sreentime_t=0;
   endtime_t=0;
   memset(&myshut,0,sizeof(myshut));
  }
  else
  {
    endtime_t=0;
    myshut=killtime;
  }

}
void timethread::beginToWork()
{
    t_time=new QTimer();
    connect(t_time,SIGNAL(timeout()),this,SLOT(ct_time()));
    t_time->setTimerType(Qt::PreciseTimer);
    t_time->start(998);

//    qDebug()<<u8"①是否活跃:"<<t_time->isActive();
//    qDebug()<<u8"②时间间隔:"<<t_time->interval();
//    qDebug()<<u8"③是否单次触发:"<<t_time->isSingleShot();
//    qDebug()<<u8"④定时器类型(针对不同平台的精确度):"<<t_time->timerType();
}
bool timethread::MyScreenShutDown(bool commond)
{
    if(m_operationworker->ScreenOperation(commond)==0)
        return true;
    else
        return false;

}
bool timethread::MySystemShutDown()
{
  if(m_operationworker->PoweroffOperation(1)==0)
      return true;
  else
      return false;
}
